package jmvm.schemes;


import java.awt.Point;
import java.util.ArrayList;

import jmvm.components.*;
import jmvm.gui.GuiLabel;
import jmvm.gui.GuiLine;
import jmvm.gui.GuiScheme;



public class Intr3 {
	
	public static BoolsToInt UINT, UEXT, IVTDSP;
	public static IntToBools BRBits;
	
	public static CD CD1, CD2;
	public static REG BR, IVTP;
	
	public static TSB UINTout, UEXTout, IVTDSPout, IVTPout;
	
	////////////
	//////////// Grafika
	////////////
	
	public static GuiScheme gui;

	public static GuiScheme getGui() {
		return gui;
	}
	
	public static void gui() {

		GuiLine line; // Pomocna promenljiva
		gui = new GuiScheme("jmvm/jmvm/images/intr3.png");
		
		ArrayList<ArrayList<Point>> sections;
		ArrayList<Point> points;
		
		// 
		// 
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(439,3));
		points.add(new Point(439,758));
		points.add(new Point(442,758));
		points.add(new Point(442,3));
		points.add(new Point(441,3));
		points.add(new Point(441,758));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(215,372));
		points.add(new Point(215,345));
		points.add(new Point(439,345));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(215,631));
		points.add(new Point(215,603));
		points.add(new Point(439,603));
		sections.add(points);
		line=new GuiLine(sections,Bus.IBUS());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(350,73));
		points.add(new Point(439,73));
		sections.add(points);
		line=new GuiLine(sections,UINTout.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(349,225));
		points.add(new Point(439,225));
		sections.add(points);
		line=new GuiLine(sections,UEXTout.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(348,547));
		points.add(new Point(439,547));
		sections.add(points);
		line=new GuiLine(sections,IVTDSPout.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(349,711));
		points.add(new Point(439,711));
		sections.add(points);
		line=new GuiLine(sections,IVTPout.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(193,73));
		points.add(new Point(320,73));
		sections.add(points);
		line=new GuiLine(sections,UINT.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(193,225));
		points.add(new Point(320,225));
		sections.add(points);
		line=new GuiLine(sections,UEXT.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(183,516));
		points.add(new Point(183,547));
		points.add(new Point(320,547));
		sections.add(points);
		line=new GuiLine(sections,IVTDSP.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(215,679));
		points.add(new Point(215,711));
		points.add(new Point(296,711));
		points.add(new Point(321,711));
		sections.add(points);
		line=new GuiLine(sections,IVTP.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(129,101));
		points.add(new Point(165,101));
		sections.add(points);
		line=new GuiLine(sections,CD1.getOut(1));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(129,120));
		points.add(new Point(165,120));
		sections.add(points);
		line=new GuiLine(sections,CD1.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(129,272));
		points.add(new Point(165,272));
		sections.add(points);
		line=new GuiLine(sections,CD2.getOut(0));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(129,254));
		points.add(new Point(165,254));
		sections.add(points);
		line=new GuiLine(sections,CD2.getOut(1));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(129,235));
		points.add(new Point(165,235));
		sections.add(points);
		line=new GuiLine(sections,CD2.getOut(2));
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,82));
		points.add(new Point(72,82));
		sections.add(points);
		line=new GuiLine(sections,Intr1.PRCOD());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,101));
		points.add(new Point(72,101));
		sections.add(points);
		line=new GuiLine(sections,Intr1.PRADR());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,120));
		points.add(new Point(72,120));
		sections.add(points);
		line=new GuiLine(sections,Intr1.PRINM());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,139));
		points.add(new Point(73,139));
		sections.add(points);
		line=new GuiLine(sections,Exec2.PSWT());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,188));
		points.add(new Point(72,188));
		sections.add(points);
		line=new GuiLine(sections,Intr2.irm7());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,207));
		points.add(new Point(72,207));
		sections.add(points);
		line=new GuiLine(sections,Intr2.irm6());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,225));
		points.add(new Point(72,225));
		sections.add(points);
		line=new GuiLine(sections,Intr2.irm5());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,244));
		points.add(new Point(72,244));
		sections.add(points);
		line=new GuiLine(sections,Intr2.irm4());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,263));
		points.add(new Point(72,263));
		sections.add(points);
		line=new GuiLine(sections,Intr2.irm3());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,282));
		points.add(new Point(72,282));
		sections.add(points);
		line=new GuiLine(sections,Intr2.irm2());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(64,301));
		points.add(new Point(72,301));
		sections.add(points);
		line=new GuiLine(sections,Intr2.irm1());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(335,87));
		points.add(new Point(335,80));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.UINTout());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(335,239));
		points.add(new Point(335,232));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.UEXTout());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(115,409));
		points.add(new Point(123,409));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.ldBR());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(335,561));
		points.add(new Point(335,554));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.IVTDSPout());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(114,667));
		points.add(new Point(122,667));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.ldIVTP());
		gui.addLine(line);

		//
		//
		sections=new ArrayList<ArrayList<Point>>();
		points=new ArrayList<Point>();
		points.add(new Point(335,726));
		points.add(new Point(335,718));
		sections.add(points);
		line=new GuiLine(sections,UpravOper.IVTPout());
		gui.addLine(line);

		//
		//
		
		gui.addLabel(new GuiLabel(178, 438, BR.getOut(0)));
		gui.addLabel(new GuiLabel(450, 400, Bus.IBUS()));
		gui.addLabel(new GuiLabel(208, 725, IVTP.getOut(0)));

	}
	/////////////
	/////////////
	/////////////
	
	public static void init() {
		
		/////
		UINT = new BoolsToInt(8, 8);
		UEXT = new BoolsToInt(8, 8);
		BRBits = new IntToBools(8, 8);
		IVTDSP = new BoolsToInt(16, 16);
		/////
		
		CD1 = new CD(4);
		CD2 = new CD(8);
		
		BR = new REG(1, "BR");
		BR.getOut(0).setIsInt();
		BR.getOut(0).setNumOfLines(8);
		
		IVTP = new REG(1, "IVTP");
		IVTP.getOut(0).setIsInt();
		IVTP.getOut(0).setNumOfLines(16);
		
		UINTout = new TSB();
		UINTout.getOut(0).setNumOfLines(8);	
		UEXTout = new TSB();
		UEXTout.getOut(0).setNumOfLines(8);
		IVTDSPout = new TSB();
		IVTDSPout.getOut(0).setNumOfLines(16);
		IVTPout = new TSB();
		IVTPout.getOut(0).setNumOfLines(16);

	}
	
	public static void connect() {
		
		//////
		UINT.setInputPin(0, CD1.getOut(0));
		UINT.setInputPin(1, CD1.getOut(1));
		for (int i=2 ; i<8; i++) UINT.setInputPin(i, new Pin(false));
		//
		UEXT.setInputPin(0, CD2.getOut(0));
		UEXT.setInputPin(1, CD2.getOut(1));
		UEXT.setInputPin(2, CD2.getOut(2));
		UEXT.setInputPin(3, new Pin(true));
		for (int i=4 ; i<8; i++) UEXT.setInputPin(i, new Pin(false));
		//
		IVTDSP.setInputPin(0, new Pin(false));
		for (int i = 1; i < 9; i++) IVTDSP.setInputPin(i, BRBits.getOut(i-1));
		for (int i = 9; i <16; i++) IVTDSP.setInputPin(i, new Pin(false));
		//
		BRBits.setInputPin(0, BR.getOut(0));
		//////

		CD1.setE(new Pin(true));
		CD1.setInputPin(0, Exec2.PSWT());
		CD1.setInputPin(1, Intr1.PRINM());
		CD1.setInputPin(2, Intr1.PRADR());
		CD1.setInputPin(3, Intr1.PRCOD());

		CD2.setE(new Pin(true));
		CD2.setInputPin(0, new Pin(false));
		CD2.setInputPin(1, Intr2.irm1());
		CD2.setInputPin(2, Intr2.irm2());
		CD2.setInputPin(3, Intr2.irm3());
		CD2.setInputPin(4, Intr2.irm4());
		CD2.setInputPin(5, Intr2.irm5());
		CD2.setInputPin(6, Intr2.irm6());
		CD2.setInputPin(7, Intr2.irm7());
		
		BR.setPinLd(UpravOper.ldBR());
		BR.setInputPin(0, Bus.IBUS_L());
		
		IVTP.setPinLd(UpravOper.ldIVTP());
		IVTP.setInputPin(0, Bus.IBUS());
		
		// TSB UINTout, UEXTout, IVTDSPout, IVTPout;

		UINTout.setCtrl(UpravOper.UINTout());
		UINTout.setInputPin(0, UINT.getOut(0));
		Bus.addOnIBUS(UINTout.getOut(0));

		UEXTout.setCtrl(UpravOper.UEXTout());
		UEXTout.setInputPin(0, UEXT.getOut(0));
		Bus.addOnIBUS(UEXTout.getOut(0));
		
		IVTDSPout.setCtrl(UpravOper.IVTDSPout());
		IVTDSPout.setInputPin(0, IVTDSP.getOut(0));
		Bus.addOnIBUS(IVTDSPout.getOut(0));
		
		IVTPout.setCtrl(UpravOper.IVTPout());
		IVTPout.setInputPin(0, IVTP.getOut(0));
		Bus.addOnIBUS(IVTPout.getOut(0));

	}
	
	// REG BR, IVTP;
	public static void ispisiRegistre() {
		System.out.println(" Registri:" + " BR=" + BR.getOut(0).getIntVal() + " IVTP=" + IVTP.getOut(0).getIntVal() );
	}

}
